#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include <string.h>

struct date
{
       int ngay;
       int thang;
       int nam;       
};
typedef struct nhanvien
{
        char ho_ten[30];
        struct date ngay_sinh;
        char don_vi[50];
        int nam_vao_lam;
        float he_so_luong;
};

nhanvien *p;
// is character
int isCharacter(char c)
{
    if((c>='a')&&(c<='z')) return 1;
    if((c>='A')&&(c<='Z')) return 1;
    return 0;
}

//cat ten
char * catTen(char  st[])
{
     int i=0;
     int j=0;
     char *st2;
     int  n = strlen(st);
     
     
     st2=(char *)malloc((n-i)*sizeof(char));     
     i=n;
          
     while( !isCharacter(st[i])) i--;
          n=i;
     while( (st[i]!=' '))
     {
       i--;
     }
     int k=0;
     for(j=i;j<=n;j++)
     {
      st2[k]=st[j];
      k++;
     }     
     st2[k]='\0';     

     return st2;
}

// cat hot
char * catHo(char st[])
{
     int i=0, j=0;
     int n = strlen(st);
     char *st2;
     st2 = (char *)malloc(n*sizeof(char));
     while(st[i]!=' ')
     {
       st2[j]=st[i];
       i++;
       j++;
     }
     st2[j]='\0';
     return st2;
}
// sap xem ten ho

//chuan hoa
void chuanhoa(char st[])
{
     int i=0, j=0;
     char *st2;
     int n= strlen(st);    
     st2=(char *)malloc(n*sizeof(char));
     
     while(i<n)
     {
               
      while((st[i]==' ')  &&(i<n))
      {
        i++;
      }
      
      if(i<n)
      {      
             st2[j] = toupper(st[i]);             
             j++;
             i++;
      }
      
     // printf("%d ",st[i]);
      
      while((st[i]!=' ')&&(i<n))
      {
      
         st2[j] = tolower(st[i]);
    //     printf("%c",st[i]);
         i++;
         j++;
      }
      
     st2[j]=' ';
     j++;
     }
     st2[j]='\0';
//     puts(st2);
     strcpy(st,st2);
}

// ham nhap dau vao // ham nhap vao danh sach nhan vien 
nhanvien *NhapDS(int n)
{
        int i,temp;
        float tam;
        p=(nhanvien*)malloc(n*sizeof(nhanvien));
        if(p==NULL)
        {
              printf("\n Loi cap phat bo nho !");
              getch();
              exit(0);      
        }        
        else
        {
               printf("\n Nhap vao thong tin ve nhan vien \n");
               for(i = 0; i < n; i++)
               {
                     printf("\n Nhan vien thu %d ",i+1);
                     //nhap vao ho ten
                     printf("\n Ho va Ten : ");
                     fflush(stdin);
                     gets((p+i)->ho_ten);
                     chuanhoa((p+i)->ho_ten);
                     //nhap vao nam cong tac
                     printf("\nDon vi cong tac :");
                     gets((p+i)->don_vi);
                     chuanhoa((p+i)->don_vi);
                     //nhap vao ngay thang nam sinh
                     printf("\n Nhap vao ngay thang nam sinh");
                     printf(" \nNgay sinh : "); 
                     scanf("%d",&temp);
                     (p+i)->ngay_sinh.ngay=temp;
                     printf("\tThang sinh :");
                     scanf("%d",&temp);
                     (p+i)->ngay_sinh.thang=temp;
                     printf("\tNam sinh :");
                     scanf("%d",&temp);
                     (p+i)->ngay_sinh.nam=temp;
                     //nhap vao nam bat dau lam
                     printf("\n Nam vao lam :");    
                     scanf("%d",&temp);
                     (p+i)->nam_vao_lam=temp;
                     //nhap vao he so luong
                     printf("\n He so luong : ");
                     scanf("%f",&tam);
                     (p+i)->he_so_luong=tam;
                    
               }  
        }
        return(0);
}

void InDS(nhanvien *p, int n)
{
          for(int i = 0; i < n; i++)
          {
                puts("\n");
                printf("\n Nhan vien thu %d",i+1);
               printf("\n\t Ho va ten : %s",(p+i)->ho_ten);
               printf("\n\t sinh  ngay :%d/%d/%d",(p+i)->ngay_sinh.ngay,(p+i)->ngay_sinh.thang,(p+i)->ngay_sinh.nam);
               printf("\n\t Ten don vi cong tac : %s",(p+i)->don_vi);
               printf("\n\t So nam cong tac :%d",(p+i)->nam_vao_lam);
               printf("\n\t He so luong :%6.3f",(p+i)->he_so_luong);
               
          }
          puts("\n");
     
}
//swap
void swap(nhanvien *sv1,nhanvien *sv2)
{
nhanvien temp;
memcpy(&temp,sv1,sizeof(nhanvien));// ham memcpy co tac dung hoan doi toan struct 
memcpy(sv1,sv2,sizeof(nhanvien));
memcpy(sv2,&temp,sizeof(nhanvien));
}
// 



// sap xep theo ho va ten
void yeu_cau_1(nhanvien *q, int n)
{
     nhanvien *p;
     p=q;
     int i,j;
     nhanvien temp;  
      
        
     for(i=0;i<n;i++)
       {
          for(j=i+1;j<n;j++)
          {
                          
            if( strcmp(catTen((p+i)->ho_ten),catTen((p+j)->ho_ten))==0 )// ten trung nhau thi so sanh ho
            {
                if(strcmp(catTen((p+i)->ho_ten),catTen((p+j)->ho_ten))>0)
                swap(p+i,p+j);   
            }
            else 
                 {
                   if( strcmp(catTen((p+i)->ho_ten),catTen((p+j)->ho_ten))>0 )// ten trung nhau thi so sanh ho
                   swap(p+i,p+j);   
                 }
       }
       }
                      
      InDS(p,n);
}
// tim kiem nhan vien theo so tham cong tac lon hon 10
void Tham_nien(nhanvien *P, int n)
{    
     for(int i = 0; i < n; i++)
     {
          int x =2010-((p+i)->nam_vao_lam);
          if( x >= 10)
          {
             printf("\n\t Ho va ten : %s",(p+i)->ho_ten);
             printf("\n\tSo Nam cong tac la :%d",x);
             puts("");    
          }
     }
}
//
//  Tim kiem nhan vien co he so luong lon hon 3
void He_So_Luong(nhanvien *p, int n)
{
     int ok=0;
     printf("\n Nhan vien co he so luong lon hon 3.0 la :\n");
     for(int i = 0; i < n; i++)
     {
            if((p+i)->he_so_luong >= 3)
            {
                   printf("\n\t Ho va ten : %s",(p+i)->ho_ten);
                   printf("\n\t He so luong :%f ",(p+i)->he_so_luong); 
                   puts("\n");                  
                   ok=1;
            }                         
     }     
     if(ok==0)
     {
        printf("\nkhong tim thay nhan vien co he so luong lon hon 3\n");
        return;
     }
     puts("\n");
     
}
//
// Tim kiem nhan vien thoe ngay thang nam sinh
void Tim_theo_ngay(nhanvien *p, int n)
{
     int temp;
     nhanvien *q;
     q=(nhanvien *)malloc(sizeof(nhanvien));
         printf("\n Tim kiem nhan vien theo ngay thang nam sinh\n");
         printf("\n\t Nhap vao ngay sinh can tim kiem:");
                      printf(" \nNgay sinh : "); 
                      scanf("%d",&temp);
                     q->ngay_sinh.ngay=temp;
                     printf("\tThang sinh :");
                     scanf("%d",&temp);
                     q->ngay_sinh.thang=temp;
                     printf("\tNam sinh :");
                     scanf("%d",&temp);
                     q->ngay_sinh.nam=temp;
          for(int i = 0; i < n; i++)
          {
               if((q->ngay_sinh.ngay==(p+i)->ngay_sinh.ngay)&&(q->ngay_sinh.thang==(p+i)->ngay_sinh.thang)&&(q->ngay_sinh.nam==(p+i)->ngay_sinh.nam))
               {
                     printf("\n\t Ho va ten : %s",(p+i)->ho_ten);
                     printf("\n\t sinh  ngay :%d/%d/%d",(p+i)->ngay_sinh.ngay,(p+i)->ngay_sinh.thang,(p+i)->ngay_sinh.nam);
                     printf("\n\t Ten don vi cong tac : %s",(p+i)->don_vi);
                     printf("\n\t So nam cong tac :%d",(p+i)->nam_vao_lam);
                     printf("\n\t He so luong :%6.3f",(p+i)->he_so_luong);
               }
              
          }
     
}
// xoa nhan vien theo ho ten nhap vao
void xoaNhanVien(nhanvien *p, int n)
{
     char hoten[30];
     printf("\n Chuong tinh xoa nhan vien theo ho ten\n");
     fflush(stdin);
     printf("\n Hay nhap vao ten nhan vien can xoa : ");
     gets(hoten);
     chuanhoa(hoten);
     for(int i = 0; i < n; i++)
     {
        if(strcmp((p+i)->ho_ten,hoten)==0)
        {
          //strcpy( (p+i)->ho_ten,(p+i+1)->ho_ten);
           strcpy((p+i)->ho_ten,(p+i+1)->ho_ten);
            memcpy( (p+i)->ho_ten,(p+i+1)->ho_ten,sizeof(nhanvien) );
            printf("\n Da xoa xong");
            n--;
        }      
        
     }
     InDS(p,n);
          
}
// tim kiem theo ho ten
void timKiemHoTen(nhanvien *p, int n)
{
     char hoten[30];
     int check = 0;
     printf("\n Chuong tinh tim kiem nhan vien theo ho ten\n");
     fflush(stdin);
     printf("\n Hay nhap vao ten nhan vien can tim kiem : ");
     gets(hoten);
     
     for(int i = 0; i < n; i++)
     {
        if(strcmp((p+i)->ho_ten,hoten)==0)
        {
             check = 1;
             printf("\n\n ------>Thong tin ve nhan vien can tim<--------");
             printf("\n\t Ho va ten :",(p+i)->ho_ten);
             printf("\n\t sinh  ngay :%d/%d/%d",(p+i)->ngay_sinh.ngay,(p+i)->ngay_sinh.thang,(p+i)->ngay_sinh.nam);
             printf("\n\t Ten don vi cong tac : %s",(p+i)->don_vi);
             printf("\n\t So nam cong tac :%d",(p+i)->nam_vao_lam);
             printf("\n\t He so luong :%6.3f",(p+i)->he_so_luong);
             puts("\n");
             
        }      
        
     }
     
}
int main()
{
    int n;

    fflush(stdin);
    printf("\n Nhap vao n =");
    scanf("%d",&n);
    NhapDS(n);
    //
    int key;
    printf("\n --->Menu chon cac hanh dong<--- ");
    printf("\n 1. In lai danh sach nhan vien duoc sap xep theo Ten(neu trung ten thi theo ho)");
    printf("\n 2. Danh sach nhan vien co tham nien cong tac den nam 2010 la 10 nam ");
    printf("\n 3. Danh sach nhan vien co he so luong tu 3.0 tro len ");
    printf("\n 4. Tim kiem nhan vien theo ho ten ");
    printf("\n 5. Tim kiem nhan vien theo ngay thang nam sinh ");
    printf("\n 6. Xoa nhan vien theo ho ten");
    printf("\n 7. Thoat !");
    while(key!=7)
    {
               printf("\n\t Nhap vao cong viec duoc chon : ");
               scanf("%d",&key);
               switch(key)
               {
                         case 1: //InDS(p,n);
                                 yeu_cau_1(p,n);
                                 break; 
                         case 2: Tham_nien(p,n);
                                 break;
                         case 3: He_So_Luong(p,n);
                                 break;
                         case 4: timKiemHoTen(p,n);
                                 break;
                         case 5:  Tim_theo_ngay(p,n);
                                 break;
                         case 6: xoaNhanVien(p,n);
                                 break;        
                         case 7: exit(0);
                                 break;
                         default: printf("\n\t\t Ban hay xem huong dan truoc khi su dung chuong trinh ! ");
               }            
    }

    


    getch();
    return (0);    
    
}
